package com.guc.md

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import com.google.android.material.appbar.AppBarLayout
import com.guc.gviewbinding.inflate
import com.guc.md.databinding.ActivityStoryBinding
import kotlin.math.abs

/**
 * Created by Guc on 2021/5/21.
 * Description：折叠标题栏
 */
class StoryActivity : AppCompatActivity() {

    private val binding: ActivityStoryBinding by inflate()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.toolbar)
        supportActionBar?.run { //显示导航按钮
            setDisplayHomeAsUpEnabled(true)
            setHomeAsUpIndicator(R.drawable.icon_back)
        }
        binding.fab.setOnClickListener {
            startActivity(Intent(this, ThirdActivity::class.java))
        }

        binding.collapsingToolbarLayout.apply {
        }
        binding.appBarLayout.apply {
            addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
                val os = abs(verticalOffset)
                when {
                    os == 0 -> {
                        Log.e("appBarLayout", "展开")
                        binding.toolbar.background = ColorDrawable(Color.parseColor("#00000000"))
                        setImmerseStatusBar()
                    }
                    os >= appBarLayout.totalScrollRange -> {
                        Log.e("appBarLayout", "关闭")
                        window.statusBarColor =
                            ResourcesCompat.getColor(resources, R.color.colorPrimaryDark, null)
                    }
                    else -> {
                        Log.e("appBarLayout", "空状态")
                    }
                }
            })
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        this.finish()
        return super.onOptionsItemSelected(item)
    }

    /**
     * 设置沉浸式状态栏
     */
    private fun setImmerseStatusBar(isDarkText: Boolean = false) { //目前只适配5.0以上系统
        // 设置图片沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            var option =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            val decorView = window.decorView
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isDarkText) {
                option = option or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window.statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = option
        }
    }

    /**
     * 设置状态栏为黑字亮色背景（默认白色）
     */
    private fun setLightStatusBar(statusBarColorResId: Int = R.color.white) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.window
                .decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            this.window.statusBarColor =
                ResourcesCompat.getColor(resources, statusBarColorResId, null)

        }
    }

}